package com.zzyy.study.service.impl;

import com.zzyy.study.entities.UserDO;
import com.zzyy.study.entities.UserDTO;
import com.zzyy.study.mapper.UserDOMapper;
import com.zzyy.study.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

@Service
@Slf4j
public class UserServiceImpl implements UserService
{
    @Resource
    private UserDOMapper userDOMapper;

    @Override
    public ResponseEntity<String> addUser(UserDTO userDTO)
    {
        //构建用户对象
        UserDO userDO = new UserDO();
        userDO.setUserName(userDTO.getUserName());
        userDO.setUserPhone(userDTO.getUserPhone());
        userDO.setPassword("123456");

        Example whereCondition = new Example(UserDO.class);
        Example.Criteria criteria = whereCondition.createCriteria();
        criteria.andEqualTo("userPhone",userDO.getUserPhone());
        UserDO userDO1 = userDOMapper.selectOneByExample(whereCondition);
        log.info("第1次userDO查询:{}",userDO1);
        if (userDO1 != null) {
            return ResponseEntity.ok("用户已存在");
        }
        //添加数据库
        int insert = userDOMapper.insertSelective(userDO);
        if (insert < 0) {
            return ResponseEntity.ok("添加用户失败");
        }
        //添加用户状态
        return ResponseEntity.ok("添加用户成功");
    }
}
